<?php

// This hook sets the locale.language and locale.lang config values
// based on the language found in the first segment of the URL.

Event::add('system.routing', 'site_lang');

function site_lang()
{
	global $lang;
	
    // Array of allowed languages
    $locales = Config::item('locale.allowed_locales');
	
	if(cookie::get('lang'))
		$lang = cookie::get('lang');
	else
		$lang = 'cn';

    // Store locale config values
    Config::set('locale.lang', $lang);
    Config::set('locale.language', $locales[$lang]);

    // Overwrite setlocale which has already been set before in Kohana::setup()
    setlocale(LC_ALL, Config::item('locale.language').'.UTF-8');

    // Finally set a language cookie for 6 months
    cookie::set('lang', $lang, 15768000);
}
 